<template>
  <div class="funny-wrap" v-if="!$common.isEmpty(funnys)">
    <div v-for="(item, index) in funnys" :key="index">
      <div style="display: flex">
        <span class="iconRotate">
          <svg style="height: 28px; width: 28px">
            <use xlink:href="#icon-fengche"/>
          </svg>
        </span>
        <span class="funny-title">{{ index + 1 }}号选手：{{ item.classify }}</span>
      </div>
      <div class="process-wrap">
        <el-collapse v-model="activeName" accordion @change="changeFunny(item.classify)">
          <el-collapse-item title="笑梗不笑人" :name="index">
            <div class="my-animation-slide-bottom"
                 style="display: flex;flex-flow: wrap;margin-left: 20px"
                 v-if="!$common.isEmpty(item.data)">
              <div style="width: 150px" v-for="(funny, i) in item.data" :key="i">
                <el-avatar class="funny-avatar myCenter" :size="110"
                           style="margin: 20px"
                           @click.native="playSound(funny.url, item.data, i)"
                           :src="funny.cover">
                </el-avatar>
                <div class="funny-item-title">{{ funny.title }}</div>
              </div>
            </div>
          </el-collapse-item>
        </el-collapse>
        <hr>
      </div>
    </div>
  </div>
</template>

<script>

export default {
  components: {},

  data() {
    return {
      pagination: {
        current: 1,
        size: 9999,
        order: "title",
        desc: false,
        resourceType: "funny",
        classify: ""
      },
      activeName: 0,
      audio: null,
      playList: null,
      index: null,
      funnys: [{
        classify: "",
        count: null,
        data: [{
          classify: "",
          cover: "",
          url: "",
          title: ""
        }]
      }],
      funny: {
        classify: "",
        title: "",
        cover: "",
        url: ""
      }
    }
  },

  computed: {},

  watch: {},

  created() {
    this.getFunny();
  },

  mounted() {

  },

  beforeDestroy() {
    if (this.audio != null && !this.audio.paused) {
      this.audio.pause();
    }
  },

  methods: {
    getFunny() {
      this.$createHttp.listFunny()
        .then((res) => {
          if (!this.$common.isEmpty(res.data)) {
            this.funnys = res.data;
            this.changeFunny(this.funnys[0].classify);
          }
        })
        .catch((error) => {
          this.$message({
            message: error,
            type: "error"
          });
        });
    },
    listFunny() {
      this.$createHttp.listResourcePath(this.pagination)
        .then((res) => {
          if (!this.$common.isEmpty(res.data) && !this.$common.isEmpty(res.data.records)) {
            this.funnys.forEach(funny => {
              if (funny.classify === this.pagination.classify) {
                funny.data = res.data.records;
                this.$forceUpdate();
              }
            });
          }
          this.pagination.classify = "";
        })
        .catch((error) => {
          this.$message({
            message: error,
            type: "error"
          });
        });
    },
    changeFunny(classify) {
      this.funnys.forEach(funny => {
        if (funny.classify === classify && this.$common.isEmpty(funny.data)) {
          this.pagination.classify = classify;
          this.listFunny();
        }
      });
    },
    playSound(src, playList, index) {
      this.playList = playList;
      this.index = index;
      if (this.audio != null) {
        if (this.audio.src === src) {
          if (this.audio.paused) {
            this.audio.play();
          } else {
            this.audio.pause();
          }
        } else {
          this.audio.pause();
          this.audio.src = src;
          this.audio.load();
          this.audio.play();
        }
      } else {
        this.audio = new Audio(src);
        this.audio.play();
        this.audio.onended = () => {
          this.index = this.index + 1;
          if (this.index < this.playList.length) {
            this.audio.src = this.playList[this.index].url;
            this.audio.load();
            setTimeout(() => {
              this.audio.play();
            }, 3000);
          }
        };
      }
    }
  }
}
</script>

<style scoped>

.funny-wrap {
  border-radius: 10px;
  max-width: 1600px;
  padding: 40px 20px 80px;
}

.funny-title {
  color: var(--black);
  font-size: 28px;
  font-weight: 700;
  margin-left: 12px;
}

.process-wrap {
  margin: 20px 0 40px;
}

.process-wrap hr {
  position: relative;
  margin: 30px auto 100px;
  border: 2px dashed var(--lightGreen);
  overflow: visible;
}

.process-wrap hr:before {
  position: absolute;
  top: -14px;
  left: 5%;
  color: var(--lightGreen);
  content: '❄';
  font-size: 30px;
  line-height: 1;
  transition: all 1s ease-in-out;
}

.process-wrap hr:hover:before {
  left: calc(95% - 20px);
}

.process-wrap >>> .el-collapse-item__header {
  border-bottom: unset;
  font-size: 20px;
  font-weight: 700;
  background-color: var(--maxMaxLightGray);
  color: var(--lightGreen);
  padding: 40px;
}

.process-wrap >>> .el-collapse-item__wrap {
  background-color: var(--maxMaxLightGray);
}

.process-wrap .el-collapse {
  border-top: unset;
  border-bottom: unset;
  border-radius: 10px;
  overflow: hidden;
}

.process-wrap >>> .el-collapse-item__wrap {
  border-bottom: unset;
}

.funny-item-title {
  text-align: center;
  margin: 0 10px;
  font-size: 18px;
  font-weight: 700;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}

.funny-item-title:hover {
  text-overflow: unset;
  overflow: unset;
}

.funny-avatar {
  cursor: pointer;
  transition: all 0.5s;
  user-select: none;
}

.funny-avatar:hover {
  transform: rotate(360deg);
}

</style>
